import requests


class HttpClient:
    # 只要用post，get请求 就会创建会话
    def __int__(self):
        self.session = requests.Session()


    def send_request(self,method,url,param_type=None,data=None,headers=None,**kwargs):
        # 请求方式转大写
        method = method.upper()
        param_type = param_type.upper()
        if method == "GET":
            res = self.session.request(method=method, url=url, params=data, **kwargs)
        elif method == "POST":

            if param_type == "FORM":
                res = self.session.request(method=method, url=url, data=data, **kwargs)
            else:
                res = self.session.request(method=method, url=url, json=data, **kwargs)
        elif method == "PUT":
            if param_type == "FORM":
                res = self.session.request(method=method, url=url, data=data, **kwargs)
            else:
                res = self.session.request(method=method, url=url, json=data, **kwargs)
        elif method == "DELET":
            if param_type == "FORM":
                res = self.session.request(method=method, url=url, data=data, **kwargs)
            else:
                res = self.session.request(method=method, url=url, json=data, **kwargs)

        else:
            raise ValueError
        return res

    # 魔法方法  send_request 实例化类。方法
    # 实例化类 HtpClient().send_request()
    # a = HttpClient()
    def __call__(self, method, url, param_type=None, data=None, headers=None, **kwargs):
        return self.send_request(method, url, param_type, data, headers, **kwargs)

    def close_session(self):
        self.session.close()
